package com.example.book.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description
 * @ClassName ManagerFilter
 * @Author 山山大魔王
 * @Date 2021/12/5 11:14
 * @Version 1.0
 */
@WebFilter(filterName = "ManagerFilter",urlPatterns = {"/pages/manager/*","/manager/bookServlet","/pages/cart/*","/cartServlet"})
public class ManagerFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        Object user = httpServletRequest.getSession().getAttribute("user");
        if (user == null){
            httpServletRequest.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }else {
            chain.doFilter(request, response);
        }


    }

    @Override
    public void destroy() {

    }
}
